clc
close all
clear all


STEP = 10;
MinWin = 20;
MaxWin = 500;
N = 111; % # of .txt files
ALPHA = 0.05;
UPPER_BOUND = [];
LOWER_BOUND = [];
% window sizes for every link
f_id = fopen ('link_txrx_status.txt');
%data = importdata (['link_txrx_status.txt']);
link_index = 1;
while feof (f_id) == 0
    link_index
    line = fgetl (f_id);
    line = fgetl (f_id);
    rx = str2num (line);
    s = rx;
    s(find(s > 0)) = 1;
    %%==============================Fixed window size ===================================
     window_size = 200;
     last_bit = length (s) - window_size;
     start_bit = 5000;
     pdr = zeros (1, last_bit - start_bit + 1);
     for i = start_bit: 1 : last_bit
       pdr(i - start_bit + 1) = sum (s (i:i+window_size))/ window_size;
     end
    %====================================================================================

    %%==============================Random Sampling =====================================
    %{
    w = [];
    window_size = 1000; %Max Window Size
    last_bit = length (s) - window_size;
    step_size = 10;
    start_bit = 5000;
    number_of_samples = 400;
    pdr = zeros (1, number_of_samples);
    for i=1:1:number_of_samples
      bit_position = round (random('Uniform',start_bit, last_bit));
      [start_index, end_index] = CalculateStationaryWindowSize (bit_position, window_size, step_size, s);
      pdr(i) = sum (s (start_index:end_index))/ (end_index - start_index + 1);
      w = [w; end_index - start_index + 1];
    end

    %}


    %====================================================================================
    dev =  norminv(1 - ALPHA / 2, 0, 1) * std(pdr) / sqrt(size(pdr, 1));
    UPPER_BOUND = [UPPER_BOUND; mean(pdr) + dev ];
    LOWER_BOUND = [LOWER_BOUND; mean(pdr) - dev ];
    link_index = link_index + 1;

end
fclose (f_id);

   
%     MIN_SAMPLE_SIZE = round(length(s)/3) ; % ensure the remaining time series is long enough
%     
%     a = length(s) - MIN_SAMPLE_SIZE;
%     coherence_wnd_sizes = zeros(1, a);
%     for i = 1 : 1 : (length(s) - MIN_SAMPLE_SIZE)
%         stationary_found = false;
%         window_low = MinWin;
%         window_high = MaxWin;
%         if length(s) - i > MaxWin
%           window_high = MaxWin;
%         end
%         if length(s) - i <= MaxWin
%           window_high = length(s) - i;
%         end
%         coherence_wnd_sizes(i) = 0;
%         
%         
%         while window_high - window_low >= 2
%           U = [window_high window_low];
%           x = s (i : i + window_high);
%           if kpsstest(x, 'alpha', .01, 'trend', false) % fail test
%               window_high = ceil ((window_high + window_low) / 2);
%           else % pass test
%               stationary_found = true;
%               coherence_wnd_sizes(i) = window_high;
%               window_high = window_high + floor ((window_high + window_low) / 2);
%               window_low = coherence_wnd_sizes(i);
%               break;
%           end
%         end
%         
%     end
%     coherence_wnd_sizes(find(coherence_wnd_sizes < MinWin)) = MinWin;
%     save(['wnd_sizes_link' num2str(file_index), '.mat'], 'coherence_wnd_sizes');
%     

cdfplot(LOWER_BOUND);
hold on;
cdfplot(UPPER_BOUND);
hold off;
